<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px"> 菜单数据列表 </span>
      <el-button @click="handleAddMenu" type="primary" plain> 添加 </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="menuTable" style="width: 100%" :data="list" v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template #default="{ row }">{{ row.id }}</template>
        </el-table-column>
        <el-table-column label="菜单名称" align="center">
          <template #default="{ row }">{{ row.title }}</template>
        </el-table-column>
        <el-table-column label="菜单级数" width="100" align="center">
          <template #default="{ row }">{{ levelFilter(row.level) }}</template>
        </el-table-column>
        <el-table-column label="前端名称" align="center">
          <template #default="{ row }">{{ row.name }}</template>
        </el-table-column>
        <el-table-column label="前端图标" width="100" align="center">
          <template #default="{ row }"><svg-icon :icon-class="row.icon"></svg-icon></template>
        </el-table-column>
        <el-table-column label="是否显示" width="100" align="center">
          <template #default="{ row }">
            <el-switch
              @change="(val) => handleHiddenChange(row, val)"
              :active-value="0"
              :inactive-value="1"
              v-model:checked="row.hidden">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template #default="{ row }">{{ row.sort }}</template>
        </el-table-column>
        <el-table-column label="设置" width="120" align="center">
          <template #default="{ row }">
            <el-button
              size="small"
              type="text"
              :disabled="disableNextLevel(row.level)"
              @click="() => handleShowNextLevel(row)">查看下级
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="{ row }">
            <el-button
              size="small"
              type="text"
              @click="() => handleUpdate(row)">编辑
            </el-button>
            <el-button
              size="small"
              type="text"
              @click="() => handleDelete(row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div> -->
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { fetchList, deleteMenu, updateMenu, updateHidden } from'../../../api';

const list = ref([]);
const total = ref(0);
const listLoading = ref(true);
const listQuery = ref({ pageNum: 1, pageSize: 5 });
const parentId = ref(0);

const getList = async () => {
  listLoading.value = true;
  try {
    const response = await fetchList(parentId.value, listQuery.value);
    list.value = response.data.list;
    console.log(list.value)
    total.value = response.data.total;
  } finally {
    listLoading.value = false;
  }
};
// const handleAddMenu = () => {
//   router.push('/ums/addMenu');
// };

const handleSizeChange = (val) => {
  listQuery.value.pageNum = 1;
  listQuery.value.pageSize = val;
  getList();
};

const handleCurrentChange = (val) => {
  listQuery.value.pageNum = val;
  getList();
};

const handleHiddenChange = async (row, value) => {
  try {
    await updateHidden(row.id, { hidden: value });
    ElMessage({
      message: '修改成功',
      type: 'success',
      duration: 1000
    });
  } catch (error) {
    // Handle error if necessary
  }
};

// const handleShowNextLevel = (row) => {
//   router.push({ path: '/ums/menu', query: { parentId: row.id } });
// };

// const handleUpdate = (row) => {
//   router.push({ path: '/ums/updateMenu', query: { id: row.id } });
// };

const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('是否要删除该菜单', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    });
    await deleteMenu(row.id);
    console.log(row.id)
    ElMessage({
      message: '删除成功',
      type: 'success',
      duration: 1000
    });
    getList();
  } catch (error) {
    // Handle cancel or error
  }
};
const levelFilter = (value) => value === 0 ? '一级' : '二级';
const disableNextLevel = (value) => value !== 0;

onMounted(() => {
  // resetParentId();
  getList();
});

</script>

<style lang="less"> 

</style>